using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using Models;
using IDAL;

namespace ITAM.Pages.Assets
{
    public class AddDeviceModel : PageModel
    {
        private IDeviceRecordService deviceRecordService;
        private IDeviceCategoryService deviceCategoryService;
        private IDepreciationRuleService depreciationRuleService;
        private IPurchasedChannelService purchasedChannelService;
        private IVendorService vendorService;

        public SelectList DeviceCategories { get; set; }
        public SelectList Vendors { get; set; }
        public SelectList PurchasedChannels { get; set; }
        public SelectList DepreciationRules { get; set; }


        [BindProperty]
        public DeviceRecord DeviceRecord { get; set; }


        public AddDeviceModel(IDeviceRecordService deviceRecordService, IDeviceCategoryService deviceCategoryService,
        IVendorService vendorService, IDepreciationRuleService depreciationRuleService, IPurchasedChannelService purchasedChannelService)
        {
            this.deviceRecordService = deviceRecordService;
            this.deviceCategoryService = deviceCategoryService;
            this.depreciationRuleService = depreciationRuleService;
            this.purchasedChannelService = purchasedChannelService;
            this.vendorService = vendorService;
        }

        public void OnGet()
        {
            var categoriesResult = deviceCategoryService.GetCategories();
            DeviceCategories = new SelectList(categoriesResult, "Id", "Name");

            var vendorResult = vendorService.GetVendors();
            Vendors = new SelectList(vendorResult, "Id", "Name");

            var purchasedChannelsRecult = purchasedChannelService.GetPurchasedChannels();
            PurchasedChannels = new SelectList(purchasedChannelsRecult, "Id", "Name");

            var depreciationRulesResult = depreciationRuleService.GetDepreciationRules();
            DepreciationRules = new SelectList(depreciationRulesResult, "Id", "Name");

        }

        public async Task<IActionResult> OnPostAsync()
        {
            DeviceRecord record = new DeviceRecord();

            if (await TryUpdateModelAsync<DeviceRecord>(
                 record,
                 "DeviceRecord",   // Prefix for form value.
                 r => r.AssetNumber,
                 r => r.CategoryId,
                 r => r.VendorId,
                 r => r.PurchaseChannelId,
                 r => r.DepreciationRuleId,
                 r => r.Description))
            {
                record.PurchaseChannelId = record.PurchaseChannelId == null ? 0 : record.PurchaseChannelId;
                record.DepreciationRuleId = record.DepreciationRuleId == null ? 0 : record.DepreciationRuleId;
                record.Description = string.IsNullOrEmpty(record.Description) ? "" : record.Description;
                record.State = "闲置";
                record.Ip = "";
                record.Mac = "";
                record.Price = 0;
                record.Photo = "";
                record.CreateAt = DateTime.Now;
                record.UpdatedAt = DateTime.Now;

                await deviceRecordService.AddDevice(record);
                return RedirectToPage("/Assets/Device");
            }

            return Page();
        }

    }
}